class LineItemsController < ApplicationController
  
  skip_before_filter :authorize, :only => :create
  
  before_action :set_line_item, only: [:show, :edit, :update, :destroy]

  # GET /line_items
  # GET /line_items.json
  def index
    @line_items = LineItem.all
  end

  # GET /line_items/1
  # GET /line_items/1.json
  def show
  end

  # GET /line_items/new
  def new
    @line_item = LineItem.new
  end

  # GET /line_items/1/edit
  def edit
  end

  
  # def create
  #     @line_item = LineItem.new(line_item_params)
  #
  #     respond_to do |format|
  #       if @line_item.save
  #         format.html { redirect_to @line_item, notice: 'Line item was successfully created.' }
  #         format.json { render :show, status: :created, location: @line_item }
  #       else
  #         format.html { render :new }
  #         format.json { render json: @line_item.errors, status: :unprocessable_entity }
  #       end
  #     end
  #   end
  
  # POST /line_items
  # POST /line_items.json
  def create
    @cart = current_cart
    
    p @cart
    p "!!!!!!!!!!!!!!"
    product = Product.find(params[:product_id])
    @line_item = @cart.add_product(product.id, product.price)
    # @line_item = @cart.add_product(product.id)

    #When something is added to the cart, reset the counter for index
    session[:counter] = 0

    respond_to do |format|
      if @line_item.save
        format.html { redirect_to(store_url) }
        format.js { @current_item = @line_item }
        format.xml { render :xml => @line_item,
                            :status => :created, :location => @line_item }
      else
        format.html { render :action => "new" }
        format.xml { render :xml => @line_item.errors,
                            :status => :unprocessable_entity }
      end
    end
  end


  # PATCH/PUT /line_items/1
  # PATCH/PUT /line_items/1.json
  def update
    respond_to do |format|
      if @line_item.update(line_item_params)
        format.html { redirect_to @line_item, notice: 'Line item was successfully updated.' }
        format.json { render :show, status: :ok, location: @line_item }
      else
        format.html { render :edit }
        format.json { render json: @line_item.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /line_items/1
  # DELETE /line_items/1.json
  def destroy
    @cart = current_cart #为调用AJAX做铺垫，没有@cart则无法调用 render @cart,进而导致错误
    if params['decrease'] == '-'#如果是-号操作      
      if @line_item.quantity >1
        @line_item.quantity -= 1
      end
      
        if @line_item.save
          # p 'entered!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
          respond_to do |format|
            format.html { redirect_to(store_url) }
            format.js { @current_item = @line_item }
            format.json { head :no_content }
          end
        end
    
    
    else
    #如果是删除操作  
    @line_item.destroy
    respond_to do |format|
      format.html { redirect_to store_url  }
      format.js
      format.json { head :no_content }
    end
   end
  end

  private
  # Use callbacks to share common setup or constraints between actions.
  def set_line_item
    @line_item = LineItem.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def line_item_params
    params.require(:line_item).permit(:product_id, :cart_id)
  end
end
